package com.AAA.controller;

import com.AAA.entity.LayData;
import com.AAA.entity.MyPage;
import com.AAA.entity.User;
import com.AAA.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

@Controller
public class UserController {
    @Autowired
    private IUserService Service;

    @RequestMapping("login")
    @ResponseBody
    public LayData login(HttpSession session, User user){
        LayData layData = new LayData();
        User user1=Service.login(user);
        if(user1!=null){
            layData.setData(true);
            // 当前的登录的信息 保存到session里面
            session.setAttribute("user",user1);

        }
        return layData;
    }

    @RequestMapping ("query")
    @ResponseBody
    public LayData query(MyPage myPage,User user){

        PageInfo pageInfo=Service.query(myPage,user);
        return new LayData(pageInfo);
    }

    @PostMapping("add")
    @ResponseBody
    public LayData add(User user){
        return new LayData(Service.add(user));
    }

    @PostMapping("upload")
    @ResponseBody
    public LayData upload(MultipartFile file, HttpServletRequest request){
        /*获取文件的名称*/
        String filename=file.getOriginalFilename();
        System.out.println("file="+file.getOriginalFilename());
        /*获取当前项目路径*/
        String realPath=request.getServletContext().getRealPath("/upload");
        System.out.println(realPath);
        File file1=new File(realPath);
        if (!file1.exists()){
            file1.mkdirs();
        }
        UUID uuid = UUID.randomUUID();
        int i = filename.lastIndexOf(".");
        String substring = filename.substring(i);
        String newname=uuid+substring;
        String picpath="";
        try {
            OutputStream outputStream=new FileOutputStream(file1+File.separator+newname);

            InputStream inputStream = file.getInputStream();

            int len=0;
            while((len=inputStream.read())!=-1){
                outputStream.write(len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            // 路径保存到数据库即可
            picpath=realPath+File.separator+newname;
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* return new LayData(picpath);*/
        return new LayData(picpath);
    }

    @PostMapping("updatas")
    @ResponseBody
    public LayData queryUser(User user){

        boolean status=Service.updatas(user);
        return new LayData(status);
    }


    @PostMapping("{username}")
    @ResponseBody
    public LayData queryUserByName(@PathVariable String username){

        boolean user=Service.queryByName(username);
        return new LayData(user);
    }

    @PostMapping("editpassword")
    @ResponseBody
    public LayData editPassword(User user){
        String  updatas = Service.editpassword(user);
        return new LayData(updatas);
    }

    @GetMapping("tuichu")
    public String tuichu(HttpSession session){
        session.invalidate();
        return "view/login";
    }
}
